home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / blf082b / pkt.c < prev    next >
C/C++ Source or Header  |  1993-11-15  |  4KB  |  193 lines

  1. /*
  2.  *    BloufGate
  3.  *    Type-2 FTS-0001 Packet Handler
  4.  *
  5.  *    Public Domain: may be copied and sold freely
  6.  */
  7.  
  8. #include    "blouf.h"
  9.  
  10. /*
  11.  *     Message ID generation
  12.  */
  13.  
  14. unsigned long mytimer=0;
  15.  
  16. unsigned long msgid_serial(void )
  17. {
  18.     if(!mytimer)
  19.         mytimer=(unsigned long) time(NULL)*16;
  20.     else
  21.         mytimer++;
  22.     return mytimer;    
  23. }
  24.  
  25.  
  26. /*
  27.  *    Write packet header
  28.  *
  29.  *    outpkt:    path to place pkt created
  30.  *    c:       config struct
  31.  *    return:    packet file desc
  32.  */
  33.  
  34. FILE *openpacket()
  35. {
  36.         int i;
  37.         char tmp[80];
  38.         struct tm *heure;
  39.         FILE *packet;
  40.         time_t starth;
  41.  
  42.         time(&starth);
  43.         
  44.         sprintf (tmp, "%s%c%8.8lx.PKT", cf->outpkt,BLF_DSEPAR, (unsigned long) starth);
  45.         
  46.         if(access(tmp,0)==0)
  47.         {
  48. /* fixme: db */ printf("pkt exists\n");
  49.             sprintf (tmp, "%s%c%8.8lx.PKT", cf->outpkt,BLF_DSEPAR,(unsigned long)(starth-111));
  50.             if(access(tmp,0)==0)
  51.             {
  52.                 logline("!Can't find a unique packet name");
  53.                 return NULL;
  54.             }
  55.         }
  56.         
  57.         packet = fopen(tmp, "wb");
  58.         
  59.         if(packet==NULL)
  60.         {
  61.             logline("!Can't open packet");
  62.             return NULL;
  63.         }
  64.         
  65.         fputiw ((WORD) cf->td_node, packet); /* orignode */
  66.         fputiw ((WORD) cf->b_node, packet); /* destnode */
  67.         heure = localtime (&starth);
  68.         fputiw ((WORD) heure->tm_year, packet);
  69.         fputiw ((WORD) heure->tm_mon, packet);
  70.         fputiw ((WORD) heure->tm_mday, packet);
  71.         fputiw ((WORD) heure->tm_hour, packet);
  72.         fputiw ((WORD) heure->tm_min, packet);
  73.         fputiw ((WORD) heure->tm_sec, packet);
  74.         fputiw (0, packet);                    /* Type 2 packet */
  75.         fputiw (2, packet);
  76.         fputiw ((WORD)cf->td_net, packet);    /* orig net */
  77.         fputiw ((WORD)cf->b_net, packet);    /* dest net */
  78.         fputc (ProdCode, packet);            /* product code */
  79.         fputc (0, packet);                    /* serial number */
  80.         
  81.         if(cf->pktpass[0]=='\0')
  82.             for (i=1; i<=8; i++) fputc (0, packet);        /* password */
  83.         else
  84.             for(i=0; i<8; i++) fputc(cf->pktpass[i], packet);
  85.                 
  86.         fputiw ((WORD)cf->o_zone, packet);                /* orig zone */
  87.         fputiw ((WORD)cf->b_zone, packet);                /* dest zone */
  88.         for (i=1; i<=20; i++) fputc (0, packet);         /* fill */
  89.         
  90.         return packet;
  91. }
  92.  
  93. /*
  94.  *    Close Packet
  95.  */
  96.  
  97. void closepacket(FILE *p)
  98. {
  99.     fputiw(0,p);
  100.     /* fputc(0,p); i was wrong */
  101.     if(ferror(p))
  102.         logline("!Write error while trying to close packet");
  103.     fclose(p);
  104. }
  105.  
  106. /*
  107.  * Open message
  108.  */
  109.  
  110. void openpktmessage(FILE *p, char *area,
  111.                BFIDOUSER *from, BFIDOUSER *to,
  112.                char *subject, unsigned long serial, struct tm *ptime)
  113. {
  114.     char timestring[BLFSTR];
  115.     time_t timer;
  116.         
  117.     time(&timer);
  118.     
  119.     fputiw (2, p);
  120.     if(area)
  121.     {
  122.         fputiw (cf->td_node, p);
  123.         fputiw (cf->b_node, p);
  124.         fputiw (cf->td_net, p);
  125.         fputiw (cf->b_net, p);
  126.     }
  127.     else
  128.     {
  129.         fputiw (from->node, p);
  130.         fputiw (to->node, p);
  131.         fputiw (from->net, p);
  132.         fputiw (to->net, p);
  133.     }
  134.  
  135.     if(area) /* attributes word */
  136.         fputiw (0, p);
  137.     else
  138.         fputiw (1, p); /* Pvt flag */
  139.         
  140.     fputiw (0, p); /* cost */
  141.  
  142.     /** DATE  **/
  143.     /* Date: 20 Dec 1990 14:48:02 */
  144.     if(!ptime) /* set to now */
  145.         strftime(timestring,40,"%d %b %y  %H:%M:%S",localtime(&timer));
  146.     else
  147.         strftime(timestring,40,"%d %b %y  %H:%M:%S",ptime);
  148.         
  149.     fputs (timestring, p);
  150.     fputc (0, p);
  151.     /** TO **/
  152.     if(to)
  153.         fputs (to->name, p);
  154.     else
  155.         fputs ("All",p);
  156.     fputc (0, p);
  157.     /** FROM **/
  158.     fputs (from->name, p);
  159.     fputc (0, p);
  160.         
  161.     /** SUBJECT **/
  162.     fputs (subject, p);
  163.     fputc (0, p);
  164.         
  165.     /** KLUDGE **/
  166.     if(area) /* Echomail, area not nil */
  167.         fprintf (p, "AREA: %s\r", area);
  168.     else
  169.     { /* netmail */
  170.         if(from->zone!=cf->o_zone || to->zone!=cf->o_zone)
  171.             fprintf(p, "\01INTL %d:%d/%d %d:%d/%d\r",
  172.                 to->zone,to->net,to->node,
  173.                 from->zone,from->net,from->node);
  174.         if(from->point)
  175.             fprintf(p, "\01FMPT %d\r", from->point);
  176.         if(to->point)
  177.             fprintf(p, "\01TOPT %d\r", to->point);
  178.     }
  179.     fprintf (p, "\01MSGID: %d:%d/%d.%d %8.8lx\r",
  180.             cf->o_zone, cf->o_net, cf->o_node, cf->o_point, serial ? serial : msgid_serial());
  181.     /* fprintf (packet, "\01PTH %d:%d/%d.%d@fidonet", zone, net, node, point); */
  182. }
  183.  
  184. /*
  185.  *    Close message packet
  186.  */
  187.  
  188. void closepktmessage(FILE *p)
  189. {
  190.     fputc(0,p);
  191. }
  192.  
  193. /* end of pkt.c */